package vincent.com.layouttest;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenzhengwen on 2015/6/21.
 */
public class CoordinatorLayoutActivity extends ActionBarActivity {
    private ViewGroup container;
//    private RecyclerView recyclerView;
    private TabLayout tab;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordinator_layout);

//        findViewById(R.id.most_outer_container).setPadding(0, getStatusBarHeight(), 0, 0);

        container = (ViewGroup)findViewById(R.id.container);
//        container.setPadding(0, getStatusBarHeight(), 0, 0);

//        recyclerView = (RecyclerView)findViewById(R.id.content_list);

        tab = (TabLayout)findViewById(R.id.tab);
        tab.addTab(tab.newTab().setText("Tab 1"));
        tab.addTab(tab.newTab().setText("Tab 2"));
        tab.addTab(tab.newTab().setText("Tab 3"));
        tab.addTab(tab.newTab().setText("Tab 4"));

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("Coordinator");

        setSupportActionBar(toolbar);

      /*  ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }*/

       /* String[] datas = new String[40];
        for(int i = 0; i < datas.length; i++){
            datas[i] = "Item " + i;
        }

        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);*/


        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        List<String> strs = new ArrayList<String>();
        for(int i = 0; i < 40; i++){
            strs.add("Item " + i);
        }
        MyAdapter adapter = new MyAdapter(this, strs);

        recyclerView.setAdapter(adapter);
    }

    public int getStatusBarHeight(){
        int height = 0;
        int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        height = getResources().getDimensionPixelSize(resId);

        return height;
    }

    private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(getSupportFragmentManager());
        adapter.addFragment(new RecyclerAdapter(), "Clases");
        adapter.addFragment(new RecyclerAdapter(), "Live");
        adapter.addFragment(new RecyclerAdapter(), "Comunidad");
        viewPager.setAdapter(adapter);
    }

    static class Adapter extends FragmentPagerAdapter {

        private final List<Fragment> mFragments = new ArrayList<Fragment>();
        private final List<String> mFragmentTitles = new ArrayList<String>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }
}
